const jwt = require('jsonwebtoken');
module.exports = () => {
    return async (ctx,next) => {
        //1.白名单
        let writePath = ['/api/login'];
        if(writePath.includes(ctx.path)){
            await next();
        }else{
            //需要校验
            let token = ctx.headers.token;
            try{
                jwt.verify(token,'1801a');
                await next();
            }catch(e){
                if(e.name === 'JsonWebTokenError' || e.name === 'TokenExpiredError'){
                    ctx.status = 401;
                    ctx.body = {
                        code:4,
                        msg:e
                    }
                }else{
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }
        }
    }
}